package zozopo.warehouse.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.comm.ZOZOPOCODE;
import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.flow.operations.FlowTB;
import zozopo.flow.operations.RecordlistTB;
import zozopo.operations.comm.OperatingService;
import zozopo.user.operations.UserTB;

public class ReceiptOS  extends OperatingService{
	
	
	public ReceiptOS(Connection conn){
		super(conn);
	}
	
	public ReceiptOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ReceiptTB> selectListBySql(String sql){
		List<ReceiptTB> receiptList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					receiptList = new ArrayList<ReceiptTB>();
					while(resultSet.next()){
						ReceiptTB receipt = new ReceiptTB();
						
						receipt.setGoodsid_receipt(resultSet.getInt(ReceiptTB.GOODSID_RECEIPT));
						receipt.setGoodsname_receipt(resultSet.getString(ReceiptTB.GOODSNAME_RECEIPT));
						receipt.setGoodunit_receipt(resultSet.getString(ReceiptTB.GOODUNIT_RECEIPT));
						receipt.setImage_receipt(resultSet.getString(ReceiptTB.IMAGE_RECEIPT));
						
						receipt.setId_receipt(resultSet.getInt(ReceiptTB.ID_RECEIPT));
						receipt.setNumber_receipt(resultSet.getInt(ReceiptTB.NUMBER_RECEIPT));
						receipt.setPrice_receipt(resultSet.getDouble(ReceiptTB.PRICE_RECEIPT));
						
						receipt.setReceiptlistid_receipt(resultSet.getInt(ReceiptTB.RECEIPTLISTID_RECEIPT));
						receipt.setTotal_receipt(resultSet.getDouble(ReceiptTB.TOTAL_RECEIPT));
						
						receipt.setProfit_receipt(resultSet.getDouble(ReceiptTB.PROFIT_RECEIPT));
						
						receipt.setReserve_receipt(resultSet.getInt(ReceiptTB.RESERVE_RECEIPT));
						receipt.setState_receipt(resultSet.getString(ReceiptTB.STATE_RECEIPT));
						
						receipt.setWarehouseid_receipt(resultSet.getInt(ReceiptTB.WAREHOUSEID_RECEIPT));
						receipt.setWarehousename_receipt(resultSet.getString(ReceiptTB.WAREHOUSENAME_RECEIPT));
						
						receipt.setUseremail_receipt(resultSet.getString(ReceiptTB.USEREMAIL_RECEIPT));
						receipt.setUsername_receipt(resultSet.getString(ReceiptTB.USERNAME_RECEIPT));
						
						receipt.setDate_receipt(resultSet.getString(ReceiptTB.DATE_RECEIPT));
						
						receipt.setStock_receipt(resultSet.getInt(ReceiptTB.STOCK_RECEIPT));
						
						receiptList.add(receipt);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return receiptList;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ReceiptTB> selectListBySqlForReport(String sql){
		List<ReceiptTB> receiptList = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					receiptList = new ArrayList<ReceiptTB>();
					while(resultSet.next()){
						ReceiptTB receipt = new ReceiptTB();
						
						receipt.setGoodsid_receipt(resultSet.getInt(ReceiptTB.GOODSID_RECEIPT));
						receipt.setGoodsname_receipt(resultSet.getString(ReceiptTB.GOODSNAME_RECEIPT));
						receipt.setGoodunit_receipt(resultSet.getString(ReceiptTB.GOODUNIT_RECEIPT));
						
						receipt.setId_receipt(resultSet.getInt(ReceiptTB.ID_RECEIPT));
						receipt.setNumber_receipt(resultSet.getInt(ReceiptTB.NUMBER_RECEIPT));
						receipt.setPrice_receipt(resultSet.getDouble(ReceiptTB.PRICE_RECEIPT));
						
						receipt.setReceiptlistid_receipt(resultSet.getInt(ReceiptTB.RECEIPTLISTID_RECEIPT));
						receipt.setTotal_receipt(resultSet.getDouble(ReceiptTB.TOTAL_RECEIPT));
						
						receipt.setProfit_receipt(resultSet.getDouble(ReceiptTB.PROFIT_RECEIPT));
						
						receipt.setReserve_receipt(resultSet.getInt(ReceiptTB.RESERVE_RECEIPT));
						receipt.setState_receipt(resultSet.getString(ReceiptTB.STATE_RECEIPT));
						
						receipt.setWarehouseid_receipt(resultSet.getInt(ReceiptTB.WAREHOUSEID_RECEIPT));
						receipt.setWarehousename_receipt(resultSet.getString(ReceiptTB.WAREHOUSENAME_RECEIPT));
						
						receipt.setUseremail_receipt(resultSet.getString(ReceiptTB.USEREMAIL_RECEIPT));
						receipt.setUsername_receipt(resultSet.getString(ReceiptTB.USERNAME_RECEIPT));
						
						receipt.setDate_receipt(resultSet.getString(ReceiptTB.DATE_RECEIPT));
						receipt.setReserve_receipt(resultSet.getInt(ReceiptTB.RESERVE_RECEIPT));
						
						//报表显示 
						receipt.setGoodsnumber(resultSet.getInt("goodsnumber"));
						receipt.setAverage(resultSet.getDouble("average"));
						receipt.setSurplusday(resultSet.getDouble("surplusday"));
						receipt.setGoodssalesnumber(resultSet.getInt("goodssalesnumber"));
						receipt.setProfit(resultSet.getDouble("profit"));
						receipt.setSumprofit(resultSet.getDouble("sumprofit"));
						
						receiptList.add(receipt);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return receiptList;
	
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insertReceipt(ReceiptTB receipt){
		SqlMachine sqlMachine = new SqlMachine(conn);
		GoodsOS goodsOS = new GoodsOS(conn);
		
		GoodsTB goods = goodsOS.getGoodsById(receipt.getGoodsid_receipt());
		int stock = goods.getNumber_goods() - receipt.getNumber_receipt();
		
		HashMap<String, Object> insertMap = new HashMap<String, Object>();
		
		insertMap.put(ReceiptTB.GOODSID_RECEIPT, receipt.getGoodsid_receipt());
		insertMap.put(ReceiptTB.GOODSNAME_RECEIPT, receipt.getGoodsname_receipt());
		insertMap.put(ReceiptTB.GOODUNIT_RECEIPT, receipt.getGoodunit_receipt());
		
		insertMap.put(ReceiptTB.IMAGE_RECEIPT, receipt.getImage_receipt()+"");
		
		insertMap.put(ReceiptTB.NUMBER_RECEIPT, receipt.getNumber_receipt());
		insertMap.put(ReceiptTB.PRICE_RECEIPT, receipt.getPrice_receipt());
		insertMap.put(ReceiptTB.RECEIPTLISTID_RECEIPT, receipt.getReceiptlistid_receipt());
		insertMap.put(ReceiptTB.REMARK_RECEIPT, receipt.getRemark_receipt());
		insertMap.put(ReceiptTB.RESERVE_RECEIPT, receipt.getReserve_receipt());
		
		
		insertMap.put(ReceiptTB.SUPPLIERID_RECEIPT, receipt.getSupplierid_receipt());
		insertMap.put(ReceiptTB.SUPPLIERNAME_RECEIPT, receipt.getSuppliername_receipt());
		insertMap.put(ReceiptTB.TOTAL_RECEIPT, receipt.getPrice_receipt() * receipt.getNumber_receipt());

		insertMap.put(ReceiptTB.USEREMAIL_RECEIPT, receipt.getUseremail_receipt());
		insertMap.put(ReceiptTB.USERNAME_RECEIPT, receipt.getUsername_receipt());
		
		insertMap.put(ReceiptTB.WAREHOUSEID_RECEIPT, receipt.getWarehouseid_receipt());
		insertMap.put(ReceiptTB.WAREHOUSENAME_RECEIPT, receipt.getWarehousename_receipt());
		
		insertMap.put(ReceiptTB.STOCK_RECEIPT, stock);
	
		return sqlMachine.insertRow(ReceiptTB.TABLE_NAME, insertMap);
	}
	
	public ReceiptTB getReceipt(int id){
		
		String sql = "SELECT * FROM " +ReceiptTB.TABLE_NAME+ " WHERE " + ReceiptTB.ID_RECEIPT + "=" +id;
		
		List<ReceiptTB> receiptList = selectListBySql(sql);
		ReceiptTB receipt = null;
		if(!(receiptList.size() == 0)){
			receipt = receiptList.get(0);
		}
		
		return receipt;
	}
	
	
	public ReceiptTB getReceiptForGoods(int goodsid){
		
		String sql = "SELECT * FROM " +ReceiptTB.TABLE_NAME+ " WHERE " + ReceiptTB.GOODSID_RECEIPT + "=" +goodsid
		+ " ORDER BY " + ReceiptTB.ID_RECEIPT +" DESC";
		
		List<ReceiptTB> receiptList = selectListBySql(sql);
		ReceiptTB receipt = null;
		if(!(receiptList.size() == 0)){
			receipt = receiptList.get(0);
		}
		
		return receipt;
	}
	
	/**
	 * @version 1.0
	 * @throws SQLException 
	 * @see 已包含业务逻辑,********** 为了保证数据正确加入线程安全需要加入单例和线程安全
	 */
	public synchronized boolean insertReceipt(List<ReceiptTB> receiptList){   
		//boolean opResult = true;
		for(ReceiptTB receipt:receiptList){
			if(!insertReceipt(receipt)){
				return false;
			}
			
			String useremail = receipt.getUseremail_receipt();
			
			int receiptId = getMaxIdByEmail(useremail);
			receipt = getReceipt(receiptId);
			
			if(!reserves(receipt)){
				return false;
			}
			
			if(!upadteRecepitPorfitByReceiptrecord(receipt)){
				return false;
			}//*/
			
			
		}

		return true;
	}
	
	/**
	 * @version 1.0
	 * @throws SQLException 
	 * @see 
	 */
	public List<ReceiptTB> selectList(int beginPageNum,int pageSize){
		
		List<ReceiptTB> receiptList = null;
		String sql = "SELECT * FROM " + ReceiptTB.TABLE_NAME  
		+ " ORDER BY " + ReceiptTB.DATE_RECEIPT + " DESC "
		+ " limit "+beginPageNum+","+pageSize; 
		receiptList = selectListBySql(sql);
		
		return receiptList;
	}
	
	/**
	 * @version 1.0
	 * @throws SQLException 
	 * @see 
	 */
	public List<ReceiptTB> selectAll(){
		
		List<ReceiptTB> receiptList = null;
		String sql = "SELECT * FROM " + ReceiptTB.TABLE_NAME;
		
		receiptList = selectListBySql(sql);
		
		return receiptList;
	}
	
	/**
	 * @version 1.0
	 * @throws SQLException 
	 * @see 
	 */
	public List<ReceiptTB> selectSalesReports(int beginPageNum,int pageSize,int day,String order,String sort){
		
		List<ReceiptTB> receiptList = null;
		String sql = "SELECT *,SUM(number_receipt) AS goodssalesnumber "
		+ ",SUM(number_receipt)/"+day+" AS average," +
			"AVG(profit_receipt) AS profit," +
			"SUM(profit_receipt) AS sumprofit," +
				"goods_warehouse.number_goods AS goodsnumber,goods_warehouse.number_goods/(SUM(number_receipt)/"+day+") AS surplusday"
		
		+ " FROM "
		+ ReceiptTB.TABLE_NAME
		
		+ " LEFT JOIN  goods_warehouse  ON  receipt_warehouse.goodsid_receipt=goods_warehouse.id_goods"

		+ " WHERE " + ReceiptTB.STATE_RECEIPT + "!=9" 
		
		+ " AND " + ReceiptTB.DATE_RECEIPT +">= DATE_ADD(NOW(), INTERVAL "+day+" DAY)"
		
		+ " GROUP BY "  + ReceiptTB.GOODSID_RECEIPT
		
		+ " ORDER BY "+order+" "+sort+" "
		
		+ " limit "+beginPageNum+","+pageSize; 
		receiptList = selectListBySqlForReport(sql);
		
		return receiptList;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<ReceiptTB> selectSalesReportsBySearchStr(String searchStr,int beginPageNum,int pageSize,int day,String order,String sort){
		List<ReceiptTB> receiptList = null;
		String sql = "SELECT *,SUM(number_receipt) AS goodssalesnumber "
		+ ",SUM(number_receipt)/"+day+" AS average," +
		" AVG(profit_receipt) AS profit," +
		"SUM(profit_receipt) AS sumprofit," +
				"goods_warehouse.number_goods AS goodsnumber,goods_warehouse.number_goods/(SUM(number_receipt)/"+day+") AS surplusday"
		
		+ " FROM "
		+ ReceiptTB.TABLE_NAME
		
		+ " LEFT JOIN  goods_warehouse  ON  receipt_warehouse.goodsid_receipt=goods_warehouse.id_goods"

		+ " WHERE " + ReceiptTB.STATE_RECEIPT + "!=9" 
		
		+ " AND " + ReceiptTB.GOODSNAME_RECEIPT +" like '%"+searchStr+"%' "
				
		+ " AND " + ReceiptTB.DATE_RECEIPT +">= DATE_ADD(NOW(), INTERVAL "+day+" DAY)"
		
		+ " GROUP BY "  + ReceiptTB.GOODSID_RECEIPT
		
		+ " ORDER BY "+order+" " + sort
		+ " limit "+beginPageNum+","+pageSize; 
		receiptList = selectListBySqlForReport(sql);
		
		return receiptList;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public Map<Integer,ReceiptTB> selectSalesReportsMapByGoodsList(List<GoodsTB> goodsList,int day){
		Map<Integer,ReceiptTB> receiptMap = null;
		List<ReceiptTB> receiptList = null;
		
		boolean isFrist = true;
		String goodsidStr = "";
		goodsidStr += "(";
		for(GoodsTB goods:goodsList){
			if(isFrist){
				goodsidStr += goods.getId_goods();
				isFrist = false;
			}else{
				
				goodsidStr += "," + goods.getId_goods();
			}
			
		}
		goodsidStr += ")";
		
		
		String sql = "SELECT *,SUM(number_receipt) AS goodssalesnumber "
		+ ",SUM(number_receipt)/"+day+" AS average," +
				" AVG(profit_receipt) AS profit," +
				"SUM(profit_receipt) AS sumprofit," +
				"goods_warehouse.number_goods AS goodsnumber,goods_warehouse.number_goods/(SUM(number_receipt)/"+day+") AS surplusday"
		
		+ " FROM "
		+ ReceiptTB.TABLE_NAME
		
		+ " LEFT JOIN  goods_warehouse  ON  receipt_warehouse.goodsid_receipt=goods_warehouse.id_goods"

		+ " WHERE " + ReceiptTB.STATE_RECEIPT + "!="+ZOZOPOCODE.CANCELED+"" 
		
		+ " AND " + ReceiptTB.GOODSID_RECEIPT +" IN "+goodsidStr+" "
				
		+ " AND " + ReceiptTB.DATE_RECEIPT +">= DATE_ADD(NOW(), INTERVAL "+day+" DAY)"
		
		+ " GROUP BY "  + ReceiptTB.GOODSID_RECEIPT;
		
	
		receiptList = selectListBySqlForReport(sql);
		if(receiptList == null){
			return null;
		}
		
		if(receiptList.size() > 0){
			receiptMap = new HashMap<Integer,ReceiptTB>();
			
			for(ReceiptTB receipt:receiptList){
				receiptMap.put(receipt.getGoodsid_receipt(), receipt);
			}
			
		}
		
		return receiptMap;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<ReceiptTB> selectBySearchStr(String searchStr,int beginPageNum,int pageSize){
		String sql = "select * from "+ReceiptTB.TABLE_NAME+" WHERE  "
		+ReceiptTB.ID_RECEIPT+" LIKE '%"+searchStr+"%'"
		+" OR "+ReceiptTB.GOODSNAME_RECEIPT+" LIKE '%"+searchStr+"%'"
		+" OR "+ReceiptTB.RECEIPTLISTID_RECEIPT+" LIKE '%"+searchStr+"%'"
		+" OR "+ReceiptTB.DATE_RECEIPT+" LIKE '%"+searchStr+"%'"
		
		+" OR "+ReceiptTB.USERNAME_RECEIPT+" LIKE '%"+searchStr+"%'"
	
		
		+ " ORDER BY " + ReceiptTB.DATE_RECEIPT + " DESC "
		
		+" limit "+beginPageNum+","+pageSize; 
		//ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		List<ReceiptTB> receiptList = selectListBySql(sql);
		
		return receiptList;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStrForSalesReports(String searchStr,int day){
		
		int rowNum = 0;
		String sql = "SELECT COUNT(DISTINCT("+ReceiptTB.GOODSID_RECEIPT+")) AS rowNum FROM " + ReceiptTB.TABLE_NAME 
		
		+ " WHERE " + ReceiptTB.STATE_RECEIPT + "!=9" 
		+ " AND " + ReceiptTB.GOODSNAME_RECEIPT +" like '%"+searchStr+"%' "
		+ " AND " + ReceiptTB.DATE_RECEIPT +">= DATE_ADD(NOW(), INTERVAL "+day+" DAY)";
		
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSalesReports(int day){
		
		int rowNum = 0;
		String sql = "SELECT COUNT(DISTINCT("+ReceiptTB.GOODSID_RECEIPT+")) AS rowNum FROM " + ReceiptTB.TABLE_NAME 
		
		+ " WHERE " + ReceiptTB.STATE_RECEIPT + "!=9" 
		
		+ " AND " + ReceiptTB.DATE_RECEIPT +">= DATE_ADD(NOW(), INTERVAL "+day+" DAY)";
		
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNumForSearchStr(String searchStr){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + ReceiptTB.TABLE_NAME +" WHERE  "
		+ReceiptTB.ID_RECEIPT+" LIKE '%"+searchStr+"%'"
		+" OR "+ReceiptTB.GOODSNAME_RECEIPT+" LIKE '%"+searchStr+"%'"
		+" OR "+ReceiptTB.RECEIPTLISTID_RECEIPT+" LIKE '%"+searchStr+"%'"
		+" OR "+ReceiptTB.DATE_RECEIPT+" LIKE '%"+searchStr+"%'"
		
		+" OR "+ReceiptTB.USERNAME_RECEIPT+" LIKE '%"+searchStr+"%'";
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}

	/**
	 * @version 1.0
	 * @throws SQLException 
	 * @see 
	 */
	public boolean reserves(ReceiptTB receipt){
		//boolean opResult = true;
		PurchaseOS purchaseOS = new PurchaseOS(conn);
		int goodid = receipt.getGoodsid_receipt();
		
		 
		if(!purchaseOS.reserves(receipt)){
			return false;
		} 
		if(!purchaseOS.updateGoodsAttribute(goodid)){
			return false;
		} 
		return true;
	}
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean updateForPorfit(ReceiptTB receipt){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
	
		
		updateMap.put(ReceiptTB.PROFIT_RECEIPT, receipt.getProfit_receipt());
		
		
		
		
		return sqlMachine.updateRow(ReceiptTB.TABLE_NAME, updateMap, ReceiptTB.ID_RECEIPT, receipt.getId_receipt());
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean upadteRecepitPorfitByReceiptrecord(ReceiptTB receipt){
		
		
		String sql = "SELECT SUM(profit_receiptrecord) as profit_receiptrecord,receiptid_receiptrecord FROM receiptrecord_warehouse "
			+" WHERE " + ReceiptrecordTB.RECEIPTID_RECEIPTRECORD + "=" + receipt.getId_receipt();
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					while(resultSet.next()){
						ReceiptrecordTB receiptrecord = new ReceiptrecordTB();
						receiptrecord.setReceiptid_receiptrecord(resultSet.getInt(ReceiptrecordTB.RECEIPTID_RECEIPTRECORD));
						receiptrecord.setProfit_receiptrecord(resultSet.getDouble(ReceiptrecordTB.PROFIT_RECEIPTRECORD));
						receipt.setProfit_receipt(receiptrecord.getProfit_receiptrecord());
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		
		
		
		if(updateForPorfit(receipt)){
			return true;
		}else{
			return false;
			
		}
		
	
	}
	
	
	
	
	
	public List<ReceiptTB> receiptInfoToReceiptList(String receiptInfoN,int receiptlistId,OperatorTB operator){
		
		
		receiptInfoN = receiptInfoN.replace("|,", "|");
		
		List<ReceiptTB> receiptList = new ArrayList<ReceiptTB>();
		
		String[] receiptArry = receiptInfoN.split("\\|");  //里面的参数第一个并不是String，而是regex，是正则式。符号“|”在正则式需要转义才能用，不然就是表示“或”的意思。用"\\|"来就可以了。

		for(String receiptInfo:receiptArry){
			String[] receiptN = receiptInfo.split(",");
			
			ReceiptTB receipt = new ReceiptTB();
			
			receipt.setWarehouseid_receipt(Integer.parseInt(receiptN[0]));
			receipt.setWarehousename_receipt(receiptN[1]);
			receipt.setGoodsid_receipt(Integer.parseInt(receiptN[2]));
			receipt.setGoodsname_receipt(receiptN[3]);
			receipt.setNumber_receipt(Integer.parseInt(receiptN[4]));
			receipt.setGoodunit_receipt(receiptN[5]);
			receipt.setPrice_receipt(Double.parseDouble(receiptN[6]));
			receipt.setTotal_receipt(receipt.getNumber_receipt()*receipt.getPrice_receipt());
			receipt.setRemark_receipt(receiptN[7]);
			
			receipt.setSuppliername_receipt(" ");
			receipt.setSupplierid_receipt(0);
			
			receipt.setUseremail_receipt(operator.getEmail_operator());
			receipt.setUsername_receipt(operator.getName_operator());
			
			receipt.setReceiptlistid_receipt(receiptlistId);
			
			//存在效率问题
			GoodsOS goodsOS = new GoodsOS(conn);
			GoodsTB goods = goodsOS.getGoodsById(receipt.getGoodsid_receipt()); 
			
			receipt.setImage_receipt(goods.getImage_goods());
			
			receipt.setStock_receipt(goods.getNumber_goods());
			
			receiptList.add(receipt);
		}
		
		
		return receiptList;
	}
	
	public List<ReceiptTB> receiptInfoToReceiptList(String receiptInfoN){
		 
		
		List<ReceiptTB> receiptList = new ArrayList<ReceiptTB>();
		
		String[] receiptArry = receiptInfoN.split("\\|");  //里面的参数第一个并不是String，而是regex，是正则式。符号“|”在正则式需要转义才能用，不然就是表示“或”的意思。用"\\|"来就可以了。

		for(String receiptInfo:receiptArry){
			String[] receiptN = receiptInfo.split(",");
			
			ReceiptTB receipt = new ReceiptTB();
			
			receipt.setWarehouseid_receipt(Integer.parseInt(receiptN[0]));
			receipt.setWarehousename_receipt(receiptN[1]);
			receipt.setGoodsid_receipt(Integer.parseInt(receiptN[2]));
			receipt.setGoodsname_receipt(receiptN[3]);
			receipt.setNumber_receipt(Integer.parseInt(receiptN[4]));
			receipt.setGoodunit_receipt(receiptN[5]);
			receipt.setPrice_receipt(Double.parseDouble(receiptN[6]));
			receipt.setTotal_receipt(receipt.getNumber_receipt()*receipt.getPrice_receipt());
			receipt.setRemark_receipt(receiptN[7]);
			

			GoodsOS goodsOS = new GoodsOS(conn);
			GoodsTB goods = goodsOS.getGoodsById(receipt.getGoodsid_receipt()); 
			//goodsOS.releaseConnection();
			
			
			receipt.setImage_receipt(goods.getImage_goods());
			
			receiptList.add(receipt);
		}
		
		
		return receiptList;
	}
	
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<ReceiptTB> getListByReceiptlistId(int receiptlistid){
		
		String sql = "SELECT * FROM " + ReceiptTB.TABLE_NAME + " where " + ReceiptTB.RECEIPTLISTID_RECEIPT + "=" + receiptlistid
		+ " ORDER BY " + ReceiptTB.ID_RECEIPT;
		
		List<ReceiptTB> receiptList = selectListBySql(sql);
		
		
		return receiptList;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<ReceiptTB> getListByInStr(String inStr){
		
		String sql = "SELECT * FROM " + ReceiptTB.TABLE_NAME + " where " + ReceiptTB.ID_RECEIPT + " IN " + inStr;
		List<ReceiptTB> receiptList = selectListBySql(sql);
		
		
		return receiptList;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxIdByEmail(String email){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(ReceiptTB.ID_RECEIPT, ReceiptTB.TABLE_NAME, ReceiptTB.USEREMAIL_RECEIPT, email);
		return maxId;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public int getRowNum(){
		
		int rowNum = 0;
		String sql = "select count(*) as rowNum from " + ReceiptlistTB.TABLE_NAME ;
		
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		if(resultSet != null){
			try {
				
				while(resultSet.next()){
					rowNum = resultSet.getInt("rowNum");
				}
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return rowNum;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public Map<Integer,ReceiptTB> getMap(){
		
		List<ReceiptTB> receiptList = selectAll();
		Map<Integer,ReceiptTB> receiptMap = new  HashMap<Integer,ReceiptTB>();
		
		for(ReceiptTB receipt:receiptList){
			receiptMap.put(receipt.getId_receipt(), receipt);
		}
		
		return receiptMap;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean cancel(int id){
		SqlMachine sqlMachine = new SqlMachine(conn);
		HashMap<String, Object> updateMap = new HashMap<String, Object>();
		
		updateMap.put(ReceiptTB.STATE_RECEIPT, ZOZOPOCODE.CANCELED);
		updateMap.put(ReceiptTB.ISCANCEL_RECEIPT, true);
		
		return sqlMachine.updateRow(ReceiptTB.TABLE_NAME, updateMap, ReceiptTB.ID_RECEIPT, id);
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean cancel(List<ReceiptTB> receiptlist){
		
		for(ReceiptTB receipt : receiptlist){
			if(!cancel(receipt.getId_receipt())){
				return false;
			}
		}
		
		return true;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public String computingProfit(){
		
		String opertionInfo = "";
		
		
		return opertionInfo;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public Map<Integer,List<ReceiptTB>> getReceiptidListMap(String inStr){
		if(inStr.length() < 3){
			return null;
		}
		
		String sql = "SELECT * FROM " + ReceiptTB.TABLE_NAME  
			+ " where " + ReceiptTB.RECEIPTLISTID_RECEIPT + " IN "+inStr+""; 
		
		List<ReceiptTB> list = new ArrayList<ReceiptTB>();
		list = selectListBySql(sql);
	
		
		
		Map<Integer,List<ReceiptTB>> valueMap = new HashMap<Integer,List<ReceiptTB>>();
		
		List<ReceiptTB> valueList = new ArrayList<ReceiptTB>();
		int value = -1;
		//int lastFlowValue = -1;
		
		for(ReceiptTB obj:list){
		//	
			if(value != -1 && value != obj.getReceiptlistid_receipt()){
				valueMap.put(value,valueList);
				valueList = new ArrayList<ReceiptTB>();
				valueList.add(obj);
				value = obj.getReceiptlistid_receipt();
			}else if(value != obj.getReceiptlistid_receipt()){
				valueList = new ArrayList<ReceiptTB>();
				valueList.add(obj);
				value = obj.getReceiptlistid_receipt();
			}else if(value == obj.getReceiptlistid_receipt()){
				valueList.add(obj);
				value = obj.getReceiptlistid_receipt();
			}
			if(obj.getReceiptlistid_receipt() == -1){
				valueMap.put(value,valueList);
				valueList = new ArrayList<ReceiptTB>();
				valueList.add(obj);
				value = obj.getReceiptlistid_receipt();
			}
		}
		
		valueMap.put(value,valueList);//修改正最后一次加入
		
		return valueMap;
	}
	

	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public List<PurchaseTB> ReceiptListToPurchaseList(List<ReceiptTB> receiptList,int backreceiptlistid){
		
		List<PurchaseTB> purchaseList = new ArrayList<PurchaseTB>();
		
		for(ReceiptTB receipt:receiptList){
			PurchaseTB purchase = new PurchaseTB();
			
			purchase.setWarehousename_purchase(receipt.getWarehousename_receipt());
			purchase.setWarehouseid_purchase(receipt.getWarehouseid_receipt());
			
			purchase.setSupplierid_purchase(0);
			purchase.setSuppliername_purchase("");
			purchase.setRemark_purchase("");
			
			purchase.setBackreceiptlistid_purchase(backreceiptlistid);
			purchase.setGoodsid_purchase(receipt.getGoodsid_receipt());
			purchase.setGoodsname_purchase(receipt.getGoodsname_receipt());
			purchase.setGoodunit_purchase(receipt.getGoodunit_receipt());
			purchase.setNumber_purchase(receipt.getNumber_receipt());
			purchase.setPrice_purchase(receipt.getPrice_receipt());
			purchase.setReserve_purchase(receipt.getNumber_receipt());
			
			purchase.setTotal_purchase(receipt.getTotal_receipt());
			purchase.setUseremail_purchase("BACKRECEIPTLIST");
			purchase.setUsername_purchase("退仓");
			
			purchaseList.add(purchase);
		}
		
		return purchaseList;
	}
	
	
}
